package com.jingdianjichi.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.enums.ISDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTyperEnum;
import com.jingdianjichi.subject.domain.convert.RadioSubjectBoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;
    @Override
    public SubjectInfoTyperEnum getHandlerType() {
        return SubjectInfoTyperEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        //单选题目插入
        List<SubjectRadio> subjectRadioList=new LinkedList<>();
        Preconditions.checkNotNull(subjectInfoBo.getOptionList(),"选项不能为空");
        try {
            subjectInfoBo.getOptionList().forEach(option->{
                SubjectRadio subjectRadio = RadioSubjectBoConverter.INSTANCE.convertAnswerBoToRadioEntity(option);
                subjectRadio.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
                subjectRadio.setSubjectId(subjectInfoBo.getId());
                subjectRadioList.add(subjectRadio);
            });
        } catch (Exception e) {
            throw new RuntimeException("单选题集合为空");
        }
        subjectRadioService.insertBatch(subjectRadioList);
    }

    @Override
    public SubjectOptionBo query(Integer subjectId) {
        List<SubjectRadio> subjectRadioList=subjectRadioService.queryByCondition(subjectId);
        List<SubjectAnswerBo> subjectAnswerBoList=RadioSubjectBoConverter.INSTANCE.convertRadioListToAnswerList(subjectRadioList);
        SubjectOptionBo subjectOptionBo=new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBoList);
        return subjectOptionBo;
    }
}
